/* Copyright (c) 2009 - 2010 98Labs Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sbal89.map.util;

import static com.sbal89.map.util.BaseUtil.SI_NAME;
import static com.sbal89.map.util.BaseUtil.U_NAME;
import static com.sbal89.map.util.BaseUtil.U_PASS;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.google.gdata.client.spreadsheet.CellQuery;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.Cell;
import com.google.gdata.data.spreadsheet.CellEntry;
import com.google.gdata.data.spreadsheet.CellFeed;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.util.ServiceException;

/**
 * This is a utility class or helper for Google's
 * Spreadsheets Data API functionalities.
 *  
 */
public class SDAUtil {

	private static final String SPREADSHEET_FEED_URL = "http://spreadsheets.google.com/feeds/spreadsheets/private/full/";
	
	/**
	 * Used for testing purposes only.
	 * 
	 * @param args
	 * @throws ServiceException 
	 * @throws IOException 
	 * @throws MalformedURLException 
	 * @throws IOException
	 * @throws ServiceException
	 */
    public static void main(String[] args) throws MalformedURLException, IOException, ServiceException {
    	SpreadsheetService myService = new SpreadsheetService(SI_NAME);
		myService.setUserCredentials(U_NAME, U_PASS);
		testDisplayCells(myService);
    }

    public static void testDisplayCells(SpreadsheetService service) throws IOException, ServiceException {
    	int minRow = 1;
    	int maxRow = 81;
    	int minCol = 1;
    	int maxCol = 8;
    	String key = "tOuWAcC2nHMfTy_HwpMok0g";
    	String row = "";
    	int i = 1;
    	for (CellEntry cell : getCells(service, key, minRow, maxRow, minCol, maxCol)) {
//    		if (row.length()>0) {
//    			row += " * ";
//    		}
    		Cell c = cell.getCell();
    		System.out.println(c.getCol()+","+c.getRow()+" " + c.getValue());
    		row += cell.getCell().getValue();
//    		if ((i%maxCol)==0) {
//    			System.out.println(row);
//    			row = "";
//    		}
    		i++;
    	}
    }
    
	public static List<CellEntry> getCells(SpreadsheetService service,
			String key, int minRow, int maxRow, int minCol, int maxCol)
			throws IOException, ServiceException {
		URL spreasheetURI = new URL(SPREADSHEET_FEED_URL + key);
		SpreadsheetEntry spreadsheetEntry = service.getEntry(spreasheetURI,
				SpreadsheetEntry.class);
		URL cellFeedUrl = spreadsheetEntry.getWorksheets().get(0)
				.getCellFeedUrl();
		CellQuery query = new CellQuery(cellFeedUrl);
		query.setMinimumRow(minRow);
		query.setMaximumRow(maxRow);
		query.setMinimumCol(minCol);
		query.setMaximumCol(maxCol);
		CellFeed feed = service.query(query, CellFeed.class);
		return feed.getEntries();
	}
    
}
